package com.moxiao.yfjh.module.activity.controller.admin.activityVerificate;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.*;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppVerificationMeltVo;
import com.moxiao.yfjh.module.activity.convert.activityVerificate.ActivityVerificateConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityVerificate.ActivityVerificateDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.moxiao.yfjh.module.activity.service.activityVerificate.ActivityVerificateService;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;
import com.moxiao.yfjh.module.activity.service.productInfo.ProductInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 相亲核销记录")
@RestController
@RequestMapping("/activity/verificate")
@Validated
public class ActivityVerificateController {

    @Resource
    private ActivityVerificateService certificateService;
    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private MerchantInfoService merchantInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建核销记录信息")
    @PreAuthorize("@ss.hasPermission('activity:verificate:create')")
    public CommonResult<Long> createVerificate(@Valid @RequestBody ActivityVerificateCreateReqVO createReqVO) {
        return success(certificateService.createVerificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新核销记录信息")
    @PreAuthorize("@ss.hasPermission('activity:verificate:update')")
    public CommonResult<Boolean> updateVerificate(@Valid @RequestBody ActivityVerificateUpdateReqVO updateReqVO) {
        certificateService.updateVerificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除核销记录信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:verificate:delete')")
    public CommonResult<Boolean> deleteVerificate(@RequestParam("id") Long id) {
        certificateService.deleteVerificate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得核销记录信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:verificate:query')")
    public CommonResult<ActivityVerificateRespVO> getVerificate(@RequestParam("id") Long id) {
        ActivityVerificateDO verificate = certificateService.getVerificate(id);
        return success(ActivityVerificateConvert.INSTANCE.convert(verificate));
    }

    @GetMapping("/list")
    @Operation(summary = "获得核销记录信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:verificate:query')")
    public CommonResult<List<ActivityVerificateRespVO>> getVerificateList(@RequestParam("ids") Collection<Long> ids) {
        List<ActivityVerificateDO> list = certificateService.getVerificateList(ids);
        return success(ActivityVerificateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得核销记录信息分页")
    @PreAuthorize("@ss.hasPermission('activity:verificate:query')")
    public CommonResult<PageResult<ActivityVerificateRespVO>> getVerificatePage(@Valid ActivityVerificatePageReqVO pageVO) {
        PageResult<ActivityVerificateDO> pageResult = certificateService.getVerificatePage(pageVO);
        return success(ActivityVerificateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出核销记录信息 Excel")
    @PreAuthorize("@ss.hasPermission('activity:verificate:export')")
    @OperateLog(type = EXPORT)
    public void exportVerificateExcel(@Valid ActivityVerificateExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<ActivityVerificateDO> list = certificateService.getVerificateList(exportReqVO);
        // 导出 Excel
        List<ActivityVerificateExcelVO> datas = ActivityVerificateConvert.INSTANCE.convertList02(list);

        ExcelUtils.write(response, "相亲分销信息.xls", "数据", ActivityVerificateExcelVO.class, datas);
    }

    /**
     * 获得当前获得的核销码列表
     */
    @GetMapping("/getVerList")
    @Operation(summary = "获得当前获得的核销码列表")
    @Parameter(name = "activityId", description = "活动id")
    public CommonResult<List<AppVerificationMeltVo>> getVerList(@RequestParam(value = "activityId") Long activityId) {
        List<AppVerificationMeltVo> list = certificateService.getVerList(activityId, getLoginUserId());
        list.forEach(item -> {
            ProductInfoDO packageDO = productInfoService.getProductInfo(item.getProductsId());
            if (Objects.nonNull(packageDO)) {
                item.setProductName(packageDO.getName());
            }
            MerchantInfoDO merchantInfoDO = merchantInfoService.getMerchantInfo(item.getMerchantId());
            if (Objects.nonNull(merchantInfoDO)) {
                item.setMerchantName(merchantInfoDO.getName());
                item.setMerchanAddress(merchantInfoDO.getAddress());
                item.setMerchantCover(merchantInfoDO.getMerchantCover());
                item.setMerchantCover(merchantInfoDO.getMerchantCover());
            }
        });
        return success(list);
    }

    /**
     * 根据报名id查看核销码
     */
    @GetMapping("/getCodeList")
    @Operation(summary = "根据报名id查看核销码")
    @Parameters({
            @Parameter(name = "activityId", description = "活动id", required = true, example = "1024"),
            @Parameter(name = "userId", description = "报名的用户id", required = true, example = "1024")
    })
    public CommonResult<List<AppVerificationMeltVo>> getCodeList(@RequestParam(value = "activityId") Long activityId,
                                                                 @RequestParam(value = "userId") Long userId,
                                                                 @RequestParam(value = "enrollId") Long enrollId) {
        List<AppVerificationMeltVo> list = certificateService.getCodeList(activityId, userId, enrollId);
        list.forEach(item -> {
            ProductInfoDO packageDO = productInfoService.getProductInfo(item.getProductsId());
            if (Objects.nonNull(packageDO)) {
                item.setProductName(packageDO.getName());
            }
            MerchantInfoDO merchantInfoDO = merchantInfoService.getMerchantInfo(item.getMerchantId());
            if (Objects.nonNull(merchantInfoDO)) {
                item.setMerchantName(merchantInfoDO.getName());
                item.setMerchanAddress(merchantInfoDO.getAddress());
                item.setMerchantCover(merchantInfoDO.getMerchantCover());
                item.setMerchantCover(merchantInfoDO.getMerchantCover());
            }
        });
        return success(list);
    }
}
